package cn.edu.lit.jsjxy.CourseExample.chapter2;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lit.jsjxy.CourseExample.R;

public class ex2_5_activity extends Activity implements View.OnClickListener
{


    ProgressBar progressBar;
    Button btnIncrease, btnDecrease, btnSubmit;
    EditText etName;
    RadioGroup rgSex;
    RadioButton rbtnMale, rbtnFemale;
    CheckBox cbReading, cbPlaying, cbMovie, cbShopping;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex2_5);

        progressBar = findViewById(R.id.progressbar01);
        btnIncrease = findViewById(R.id.btnIncrease);
        btnDecrease = findViewById(R.id.btnDecrease);

        btnSubmit = findViewById(R.id.btnSubmit);
        rgSex = findViewById(R.id.rgSex);
        rbtnFemale = findViewById(R.id.rbtnFemale);
        rbtnMale = findViewById(R.id.rbtnMale);

        cbReading = findViewById(R.id.cbReading);
        cbPlaying = findViewById(R.id.cbPlaying);

        cbMovie = findViewById(R.id.cbMovie);

        cbShopping = findViewById(R.id.cbShopping);
        etName = findViewById(R.id.etName);

        //cbReading, cbPlaying, cbMovie,cbShopping


        btnIncrease.setOnClickListener(this);
        btnDecrease.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);

    }

    public void showPersonMessage()
    {
        String name = etName.getText().toString().trim();
        String sex = "";
        String hobbies = "";

        if (name.equals("")) {
            Toast.makeText(this, "input your name please!", Toast.LENGTH_SHORT).show();
            return;
        }

        switch (rgSex.getCheckedRadioButtonId()) {
            case R.id.rbtnMale:
                sex = "male";
                break;
            case R.id.rbtnFemale:
                sex = "female";
                break;
        }

        if (cbReading.isChecked()) {
            hobbies += "Reading,";
        }
        if (cbPlaying.isChecked()) {
            hobbies += "playing,";
        }
        if (cbMovie.isChecked()) {
            hobbies += "movie,";
        }
        if (cbShopping.isChecked()) {
            hobbies += "shopping";
        }

        if (hobbies.charAt(hobbies.length() - 1) == ',') {
            hobbies = hobbies.substring(0, hobbies.length() - 1);
        }

        TextView tv = new TextView(this);
        tv.setText(name + "\n" + sex + "\n" + hobbies);
        tv.setTextSize(24);
        tv.setTextColor(Color.RED);
        tv.setBackgroundColor(Color.BLACK);

        Toast toast = new Toast(this);


        toast.setView(tv);

        toast.show();
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.btnIncrease:
                progressBar.incrementProgressBy(5);
                progressBar.incrementSecondaryProgressBy(2);

                int progress1 = progressBar.getProgress();
                if (progress1 >= progressBar.getMax()) {
                    Toast.makeText(this, progress1 + "", Toast.LENGTH_SHORT).show();
                }

                break;

            case R.id.btnDecrease:
                progressBar.incrementProgressBy(-5);
                progressBar.incrementSecondaryProgressBy(-2);

                int progress2 = progressBar.getProgress();

                if (progress2 <= 0) {
                    Toast.makeText(this, progress2 + "", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btnSubmit:

                showPersonMessage();
                break;
        }

    }
}
